package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchasingDistributionDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchasingDistribution;
import com.guodu.entity.PurchasingdisStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchasingDistributionService;
import com.guodu.vo.PurchasingDistributionQueryVo;
import com.guodu.vo.PurchasingDistributionVo;


@Service("purchasingDistributionService")
public class PurchasingDistributionServiceImpl implements PurchasingDistributionService {

	
	
	@Resource
	private PurchasingDistributionDao purchasingDistributionDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	
	
	@Override
	public void save(PurchasingDistribution purchasingDistribution) {
		// TODO Auto-generated method stub
		purchasingDistributionDao.save(purchasingDistribution);
	}

	@Override
	public List<PurchasingDistributionVo> findByVo(
			PurchasingDistributionQueryVo pdq, Units units,UserInfo userInfo, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		List<PurchasingDistribution> list = purchasingDistributionDao.findByVo(pdq,units,userInfo, curPage, pageSize);
		List<PurchasingDistributionVo> purchasingDistributionVos = new ArrayList<PurchasingDistributionVo>();
		for(PurchasingDistribution purchasingDistribution : list ){
			PurchasingDistributionVo purchasingDistributionVo = new PurchasingDistributionVo(purchasingDistribution);
			UserInfo createUser = userInfoDao.findById(purchasingDistribution.getCreatePeople());
			if(purchasingDistribution.getUnitsId()!=null){
				Units u = unitsDao.findById(purchasingDistribution.getUnitsId());
					if(u.getUnitsLevel()==2L){
						Units fu = unitsDao.findById(u.getFunitsId());
						purchasingDistributionVo.setFunitsName(fu.getUnitsName());
						purchasingDistributionVo.setUnitsName(u.getUnitsName());
						purchasingDistributionVo.setIndustry(u.getIndustry());
					}else{
						purchasingDistributionVo.setFunitsName(u.getUnitsName());
					}
				}
				if (createUser.getUnitsId()!=null) {
					Units unitsInfos = unitsDao.findById(createUser.getUnitsId());
					purchasingDistributionVo.setIsComplete(unitsInfos.getIsComplete());
					purchasingDistributionVo.setUnitsLevel(unitsInfos.getUnitsLevel());
				}else{
					Long id = null;
					purchasingDistributionVo.setUnitsLevel(id);
				}
				purchasingDistributionVo.setUserName(createUser.getUserName());
				purchasingDistributionVos.add(purchasingDistributionVo);
		}
		return purchasingDistributionVos;
	}

	@Override
	public long findSizeByVo(PurchasingDistributionQueryVo pdq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		return purchasingDistributionDao.findSizeByVo(pdq,units,userInfo);
	}

	@Override
	public PurchasingDistribution findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingDistributionDao.findById(purchaseId);
	}

	@Override
	public void update(PurchasingDistribution purchasingDistribution,Long status) {
		// TODO Auto-generated method stub
		purchasingDistributionDao.update(purchasingDistribution);
		List<PurchasingdisStatus> entities = new ArrayList<PurchasingdisStatus>();
		PurchasingdisStatus entity = new PurchasingdisStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchasingDistribution.getCreatePeople());
		entity.setPurchaseId(purchasingDistribution.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchasingDistributionDao.changeStatus(entities);
	}

	@Override
	public List<PurchasingdisStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingDistributionDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasingdisStatus> entities = new ArrayList<PurchasingdisStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasingdisStatus entity = new PurchasingdisStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchasingDistributionDao.changeStatus(entities);
		purchasingDistributionDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchasingDistributionDao.delByIds(delIds);
	}

	@Override
	public List<PurchasingDistribution> findAllByVo(
			PurchasingDistributionQueryVo pdq, Units attribute,UserInfo userInfo) {
		// TODO Auto-generated method stub
		return purchasingDistributionDao.findAllByVo(pdq,attribute,userInfo);
	}

	@Override
	public PurchasingDistributionVo findAllMoney(
			PurchasingDistributionQueryVo pdq, Units units, UserInfo userInfo) {
		// TODO Auto-generated method stub
		return purchasingDistributionDao.findAllMoney(pdq, units, userInfo);
	}

	@Override
	public void examinAll(PurchasingDistributionQueryVo pdq, Units units,
			UserInfo userInfo) {
		// TODO Auto-generated method stub
		purchasingDistributionDao.examinAll(pdq,units,userInfo);
	}

}
